package com.jeecg.demo.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.jeecg.demo.entity.ListobjsEntity;
import com.jeecg.demo.entity.ShopsEntity;
import com.jeecg.demo.service.ListobjsServiceI;
import com.jeecg.demo.util.GetUserTenantId;
import com.jeecg.demo.util.PopupDataBindUtil;

/**
 * @Title: Controller
 * @Description: LISTOBJS
 * @author onlineGenerator
 * @date 2018-11-26 11:26:37
 * @version V1.0
 * 
 */
@Controller
@RequestMapping("/listobjsController")
public class ListobjsController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(ListobjsController.class);

	@Autowired
	private ListobjsServiceI listobjsService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private PopupDataBindUtil bindUtil;

	/**
	 * LISTOBJS列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/jeecg/demo/listobjsList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @throws Exception
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(ListobjsEntity listobjs, HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) throws Exception {
		CriteriaQuery cq = new CriteriaQuery(ListobjsEntity.class, dataGrid);
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				listobjs, request.getParameterMap());
		cq.eq("objtype", "1");
		cq.add();
		try {
			// 自定义追加查询条件
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		GetUserTenantId getuserfun=new GetUserTenantId();
//		String idString = String.valueOf(systemService.findOneForJdbc(getuserfun.getSql(),getuserfun.getOrgcode()).get("worktypeid"));  
//		int worktypeid = Integer.parseInt(idString);
//		cq.eq("worktypeid", worktypeid);
		cq.add();
		this.listobjsService.getDataGridReturn(cq, true);
		// 绑定非字典表数据
		bindUtil.BindDDName(dataGrid, "shops", "shopname", "shopid", "objid",
				"shopname");
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除LISTOBJS
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(ListobjsEntity listobjs, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		listobjs = systemService.getEntity(ListobjsEntity.class,
				listobjs.getId());
		message = "删除成功";
		try {
			listobjsService.delete(listobjs);
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 批量删除LISTOBJS
	 * 
	 * @return
	 */
	@RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "删除成功";
		try {
			for (String id : ids.split(",")) {
				ListobjsEntity listobjs = systemService.getEntity(
						ListobjsEntity.class, Integer.parseInt(id));
				listobjsService.delete(listobjs);
				systemService.addLog(message, Globals.Log_Type_DEL,
						Globals.Log_Leavel_INFO);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 添加LISTOBJS
	 *
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(ListobjsEntity listobjs, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "添加成功";
		try {
			listobjs.setObjtype("1");
			listobjsService.save(listobjs);
			systemService.addLog(message, Globals.Log_Type_INSERT,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 更新LISTOBJS
	 *
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(ListobjsEntity listobjs, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "更新成功";
		ListobjsEntity t = listobjsService.get(ListobjsEntity.class,
				listobjs.getId());
		try {
			t.setObjtype("1");
			MyBeanUtils.copyBeanNotNull2Bean(listobjs, t);
			listobjsService.saveOrUpdate(t);
			systemService.addLog(message, Globals.Log_Type_UPDATE,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * LISTOBJS新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(ListobjsEntity listobjs, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(listobjs.getId())) {
			listobjs = listobjsService.getEntity(ListobjsEntity.class,
					listobjs.getId());
			req.setAttribute("listobjsPage", listobjs);
		}
		return new ModelAndView("com/jeecg/demo/listobjs-add");
	}

	/**
	 * LISTOBJS编辑页面跳转
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(ListobjsEntity listobjs, HttpServletRequest req) throws Exception {
		if (StringUtil.isNotEmpty(listobjs.getId())) {
			listobjs = listobjsService.getEntity(ListobjsEntity.class, listobjs.getId());
			Object obj = listobjs;
			// 绑定非字典表数据
			Map<String, Object> resultList = bindUtil.BindDDName(obj, "shops", "shopname", "shopid", "objid", "shopname");
			req.setAttribute("listobjsPage", resultList);
		}
		return new ModelAndView("com/jeecg/demo/listobjs-update");
	}

	/**
	 * 导入功能跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name", "listobjsController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}

	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(ListobjsEntity listobjs,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid, ModelMap modelMap) {
		CriteriaQuery cq = new CriteriaQuery(ListobjsEntity.class, dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				listobjs, request.getParameterMap());
		List<ListobjsEntity> listobjss = this.listobjsService
				.getListByCriteriaQuery(cq, false);
		modelMap.put(NormalExcelConstants.FILE_NAME, "LISTOBJS");
		modelMap.put(NormalExcelConstants.CLASS, ListobjsEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams(
				"LISTOBJS列表", "导出人:"
						+ ResourceUtil.getSessionUser().getRealName(), "导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, listobjss);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

	/**
	 * 导出excel 使模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(ListobjsEntity listobjs,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid, ModelMap modelMap) {
		modelMap.put(NormalExcelConstants.FILE_NAME, "LISTOBJS");
		modelMap.put(NormalExcelConstants.CLASS, ListobjsEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams(
				"LISTOBJS列表", "导出人:"
						+ ResourceUtil.getSessionUser().getRealName(), "导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, new ArrayList());
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request,
			HttpServletResponse response) {
		AjaxJson j = new AjaxJson();

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<ListobjsEntity> listListobjsEntitys = ExcelImportUtil
						.importExcel(file.getInputStream(),
								ListobjsEntity.class, params);
				for (ListobjsEntity listobjs : listListobjsEntitys) {
					listobjsService.save(listobjs);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(e.getMessage());
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}

}
